package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ArticuloSubGrupo;

public class ArticuloSubGrupoDAOImpl extends JdbcDaoSupport implements IArticuloSubGrupoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    protected FindListByCriteriaQuery findListByCriteriaQuery;
    
    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_ARTICULO_SUB_GRUPO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ArticuloSubGrupo articuloSubGrupo) {
		deleteOperation.execute( articuloSubGrupo  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ArticuloSubGrupo findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ArticuloSubGrupo articuloSubGrupo) {
		insertOperation.execute( articuloSubGrupo );
	}

	public void update(ArticuloSubGrupo articuloSubGrupo) {
		updateOperation.execute( articuloSubGrupo );
	}

	public ArticuloSubGrupo findByCriteria(ArticuloSubGrupo  articuloSubGrupo) {
		return findByCriteriaQuery.runQuery(articuloSubGrupo);
	}

	public List load(ArticuloSubGrupo  articuloSubGrupo) {
		return findAllQuery.runQuery(articuloSubGrupo);
	}

	public ArticuloSubGrupo findByActiveState(ArticuloSubGrupo  articuloSubGrupo) {
		return findByActiveStateQuery.runQuery(articuloSubGrupo);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findListByCriteriaQuery = new FindListByCriteriaQuery( getDataSource() );
    	//Deactivated Operations
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_ARTICULO_SUB_GRUPO_T ("
				  + " ID, "
				  + " ID_ARTICULO_GRUPO, "
				  + " CODIGO, "
				  + " NOMBRE, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_ARTICULO_SUB_GRUPO_SEC.nextVal, "
			  	  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(ArticuloSubGrupo articuloSubGrupo) {
			InsertOperation.this.update( new Object[]{
				articuloSubGrupo.getIdArticuloGrupo(),
				articuloSubGrupo.getCodigo(),
				articuloSubGrupo.getNombre(),
				articuloSubGrupo.getEstado(),
				articuloSubGrupo.getUsuarioCrea()
			});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_ARTICULO_SUB_GRUPO_T SET "
					  + " ID_ARTICULO_GRUPO = ? ,"
					  + " CODIGO = ? ,"
					  + " NOMBRE = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
			  	  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ArticuloSubGrupo articuloSubGrupo) {
			UpdateOperation.this.update( new Object[]{
				articuloSubGrupo.getIdArticuloGrupo(),
				articuloSubGrupo.getCodigo(),
				articuloSubGrupo.getNombre(),
				articuloSubGrupo.getEstado(),
				articuloSubGrupo.getUsuarioModifica(),
				articuloSubGrupo.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_ARTICULO_SUB_GRUPO_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(ArticuloSubGrupo articuloSubGrupo) {
			this.update( new Object[]{
				articuloSubGrupo.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_ARTICULO_SUB_GRUPO_T "
										+ WHERE + " 1=1 "
			  	  	  					+ " ID = ? ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
					new Long(id)
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArticuloSubGrupo articuloSubGrupo = new ArticuloSubGrupo();
				articuloSubGrupo.setId(new Long(rs.getLong("ID")));
				articuloSubGrupo.setIdArticuloGrupo(new Long(rs.getLong("ID_ARTICULO_GRUPO")));
				articuloSubGrupo.getArticuloGrupo().setNombre(rs.getString("ARTICULO_GRUPO"));
				articuloSubGrupo.setCodigo(rs.getString("CODIGO"));
				articuloSubGrupo.setNombre(rs.getString("NOMBRE"));
				articuloSubGrupo.setEstado(rs.getString("ESTADO"));
				articuloSubGrupo.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				articuloSubGrupo.setFechaCrea(rs.getDate("FECHA_CREA"));		
				articuloSubGrupo.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				articuloSubGrupo.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return articuloSubGrupo;
		}
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperationMasterListItems extends MappingSqlQuery {
		public BaseQueryOperationMasterListItems(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArticuloSubGrupo articuloSubGrupo = new ArticuloSubGrupo();
			articuloSubGrupo.setId(new Long(rs.getLong("ID")));
			articuloSubGrupo.setCodigo(rs.getString("CODIGO"));
			articuloSubGrupo.setNombre(rs.getString("NOMBRE"));
			return articuloSubGrupo;
		}
    }

	// ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = "SELECT "		
										 + "A.ID, "
										 + "A.ID_ARTICULO_GRUPO, "
										 + "B.NOMBRE AS ARTICULO_GRUPO, "
										 + "A.CODIGO, "
										 + "A.NOMBRE, "
										 + "A.ESTADO, "
										 + "A.USUARIO_CREA, "
										 + "A.FECHA_CREA, "
										 + "A.USUARIO_MODIFICA, "
										 + "A.FECHA_MODIFICA "
										 + "FROM GES_ARTICULO_SUB_GRUPO_T A "
										 + "INNER JOIN "
										 + "GES_ARTICULO_GRUPO_T B "
										 + "ON "
										 + "A.ID_ARTICULO_GRUPO = B.ID "
										 + WHERE + " 1=1 "
			  	  	  					 + " AND ID_ARTICULO_GRUPO = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public ArticuloSubGrupo runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ArticuloSubGrupo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID_ARTICULO_GRUPO = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND NOMBRE = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ArticuloSubGrupo runQuery(ArticuloSubGrupo articuloSubGrupo) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//articuloSubGrupo.getIdArticuloGrupo(),
				//articuloSubGrupo.getCodigo(),
				//articuloSubGrupo.getNombre(),
				//articuloSubGrupo.getEstado(),
				//articuloSubGrupo.getUsuarioCrea(),
				//articuloSubGrupo.getFechaCrea(),
				//articuloSubGrupo.getUsuarioModifica(),
				//articuloSubGrupo.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ArticuloSubGrupo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindListByCriteriaQuery ---------------------------------    
    public class FindListByCriteriaQuery extends BaseQueryOperationMasterListItems {
		private static final String sql = " SELECT "
										+ " ID, CODIGO, NOMBRE "
										+ " FROM "
										+ " GES_ARTICULO_SUB_GRUPO_T "
										+ " WHERE 1=1 "
			  	  	  					+ " AND ID_ARTICULO_GRUPO = ? "
			  	  	  					+ " AND ESTADO = ? "
										+ "";
    	public FindListByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ArticuloSubGrupo articuloSubGrupo) {
			List result =  execute( new Object[]{
					articuloSubGrupo.getIdArticuloGrupo(),
					articuloSubGrupo.getEstado()
				});
			if ( result != null && result.size() > 0 ) {
        		return result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = "SELECT "		
			 + "A.ID, "
			 + "A.ID_ARTICULO_GRUPO, "
			 + "B.NOMBRE AS ARTICULO_GRUPO, "
			 + "A.CODIGO, "
			 + "A.NOMBRE, "
			 + "A.ESTADO, "
			 + "A.USUARIO_CREA, "
			 + "A.FECHA_CREA, "
			 + "A.USUARIO_MODIFICA, "
			 + "A.FECHA_MODIFICA "
			 + "FROM GES_ARTICULO_SUB_GRUPO_T A "
			 + "INNER JOIN "
			 + "GES_ARTICULO_GRUPO_T B "
			 + "ON "
			 + "A.ID_ARTICULO_GRUPO = B.ID "
			 + WHERE 
			 + " 1=1 "
			 + " AND UPPER(A.CODIGO) LIKE ? "
			 + " AND UPPER(A.NOMBRE) LIKE ? "
			 + " AND A.ESTADO LIKE ? "
			 + " AND A.ID_ARTICULO_GRUPO = ? ";
		public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public List runQuery(ArticuloSubGrupo articuloSubGrupo) {
			List result =  execute(new Object[]{
					new String("%" + (articuloSubGrupo.getCodigo() != null && !articuloSubGrupo.getCodigo().equals("") ? articuloSubGrupo.getCodigo().toUpperCase() : "%") + "%"),
					new String("%" + (articuloSubGrupo.getNombre() != null && !articuloSubGrupo.getNombre().equals("") ? articuloSubGrupo.getNombre().toUpperCase() : "%") + "%"),
					new String("%" + (articuloSubGrupo.getEstado() != null && !articuloSubGrupo.getEstado().equals("") ? articuloSubGrupo.getEstado().toUpperCase() : "%") + "%"),
					articuloSubGrupo.getIdArticuloGrupo()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ArticuloSubGrupo runQuery(ArticuloSubGrupo articuloSubGrupo) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ArticuloSubGrupo) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
